package com.ysd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.YongDao;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

@WebServlet("/YongServlet")
public class YongServlet extends BaseServlet {
	YongDao dao=new YongDao();
	public void selectAll(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("us", dao.getAllUsers());
		request.getRequestDispatcher("houtai/YongList.jsp").forward(request, response);
		
	}
	public void selectAllJson(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int page=Integer.parseInt(request.getParameter("page").toString());
		int size=Integer.parseInt(request.getParameter("limit").toString());
		String name=null;
		if(request.getParameter("name")!=null &&request.getParameter("name")!="") {
			name=request.getParameter("name").toString();
		}
		ArrayList<com.ysd.entity.YongHu> ls=dao.getAllUsers();
		ArrayList<com.ysd.entity.YongHu> lsdate=dao.getAllUsersPage(page,size,name);
		Result res=new Result(0,"",ls.size(),lsdate);
		Gson g=new Gson();
		PrintWriter out=response.getWriter();
		out.print(g.toJson(res));
		out.flush();
		out.close();
	}
	public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name").toString();
		String sign=request.getParameter("sign").toString();
		String sex=request.getParameter("sex").toString();
		String pwd=request.getParameter("pwd").toString();
		String phone=request.getParameter("phone").toString();
		String email=request.getParameter("email").toString();
		int i=dao.add(name,sign,sex,pwd, phone, email);
		Result res=new Result(200,"true",i,"success");
		Gson g=new Gson();
		PrintWriter out=response.getWriter();
		out.print(g.toJson(res));
		out.flush();
		out.close();
	}
	public void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name").toString();
		String sign=request.getParameter("sign").toString();
		String sex=request.getParameter("sex").toString();
		String phone=request.getParameter("phone").toString();
		String email=request.getParameter("email").toString();
		int id=Integer.parseInt(request.getParameter("id").toString());
		
		int i=dao.update(id, name, sign, sex, phone, email);
		Result res=new Result(0,"true",i,"success");
		Gson g=new Gson();
		PrintWriter out=response.getWriter();
		out.print(g.toJson(res));
		out.flush();
		out.close();
	}
	public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id").toString());
		int i=dao.delete(id);
		Result res=new Result(0,"true",i,"success");
		Gson g=new Gson();
		PrintWriter out=response.getWriter();
		out.print(g.toJson(res));
		out.flush();
		out.close();
	}
}
